import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Wang Chen Peng
 * @date 2019/01/10
 * describe:加一
 * https://leetcode-cn.com/explore/featured/card/top-interview-questions-easy/1/array/27/
 */
public class AddOne {

    public static void main(String[] args) {
        //int[] arg = new int[]{7, 2, 8, 5, 0, 9, 1, 2, 9, 5, 3, 6, 6, 7, 3, 2, 8, 4, 3, 7, 9, 5, 7, 7, 4, 7, 4, 9, 4, 7, 0, 1, 1, 1, 7, 4, 0, 0, 6};
        //int[] arg = new int[]{9, 9};
        int[] arg = new int[]{1,9,8,9};
        System.out.println(Arrays.toString(plusOne(arg)));
    }


    static int len;
    static int index;
    static boolean flag = true;

    /**
     * 自测过 提交报错  网站bug
     */
    static int[] test1(int[] arg) {
        if (flag) {
            index = arg.length - 1;
            if (arg[index] != 9) {
                arg[index] += 1;
                return arg;
            } else {
                len = arg.length;
                flag = false;
                arg[index] = 0;
                index -= 1;
                return test1(arg);
            }
        } else {
            if (index >= 0) {
                if (arg[index] != 9) {
                    arg[index] += 1;
                    return arg;
                } else if (arg[index] != 0) {
                    arg[index] = 0;
                    index -= 1;
                    return test1(arg);
                }
            } else {
                int[] result = new int[len + 1];
                result[0] = 1;
                return result;
            }
        }
        return null;
    }

    /**
     * 通过
     */
    static int[] test3(int[] arg) {
        int[] result = new int[arg.length + 1];
        boolean flag = false;
        for (int index = arg.length - 1; index >= 0; index--) {
            if (flag) {
                if (arg[index] <9) {
                    arg[index] += 1;
                    return arg;
                }else{
                    arg[index] = 0;
                    result[index + 1] = 0;
                }
            } else {
                if (arg[index] <9) {
                    arg[index] += 1;
                    return arg;
                } else {
                    flag = true;
                    arg[index] = 0;
                }
            }
        }
        result[0] = 1;
        return result;
    }

    /**
     * 官方示例
     */
    public static int[] plusOne(int[] digits) {
        for (int i = digits.length - 1; i >= 0; i--) {
            if (digits[i] == 9) {
                if (i == 0) {
                    int[] result = new int[digits.length + 1];
                    result[0] = 1;
                    return result;
                }
                digits[i] = 0;
            } else {
                digits[i] += 1;
                break;
            }
        }
        return digits;
    }

    /**
     * 失败的思路 输入数组长度不定
     */
    static int[] test2(int[] arg) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < arg.length; i++) {
            stringBuffer.append(arg[i]);
        }
        String str = stringBuffer.toString();
        Long integer = Long.valueOf(str);
        integer += 1;
        str = String.valueOf(integer);
        //ArrayList<Integer> ints = new ArrayList<>();
        arg = new int[str.length()];
        for (int i = 0; i < str.length(); i++) {
            int temp = Integer.parseInt(str.substring(i, i + 1));
            arg[i] = temp;
        }
        return arg;
    }


}
